/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on Feb 11, 2011, 5:01:35 PM
 */

package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;

import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.Highlighter;

import communication.TwoWaySerialComm;

/**
 *
 * @author Eike
 */
public class MainWindow extends javax.swing.JFrame implements Observer{

	TwoWaySerialComm comm;
	boolean connected;
	
    /** Creates new form NewJFrame */
    public MainWindow() {
    	connected = false;
    	comm = new TwoWaySerialComm();
    	comm.addObserver(this);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        lblPort = new javax.swing.JLabel();
        tfPort = new javax.swing.JTextField();
        btnConnect = new javax.swing.JButton();
        lblConnected = new javax.swing.JLabel();
        taRead = new javax.swing.JTextArea();
        scrollPaneStatus = new javax.swing.JScrollPane();
        lblStatus = new javax.swing.JLabel();
        lblSend = new javax.swing.JLabel();
        btnStart = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        cbPort = new JComboBox();
        cbResolution = new JComboBox();
        lblResolution = new JLabel();
        pbProgressBar = new JProgressBar();

        pbProgressBar.setValue(0);
        pbProgressBar.setStringPainted(true);
        
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblPort.setText("Port:");
//      cbPort.setPreferredSize(new Dimension(800, 20));
        for(int i = 0; i < comm.getPorts().size(); i++) {
        	cbPort.addItem(comm.getPorts().get(i));
        }

        lblResolution.setText("Resolution:");
        for (int i = 0; i < RESOLUTIONS.length; i++) {
        	cbResolution.addItem(RESOLUTIONS[i]);
		}
        
        tfPort.setText("COM7");
        
        
        btnStart.setText("Start");
        btnStart.setEnabled(false);
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartListener(evt);
            }
        });

        btnConnect.setText("(Dis-) Connect");
        btnConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectListener(evt);
            }
        });
        
        btnStop.setText("Stop");
        btnStop.setEnabled(false);
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopListener(evt);
            }
        });

        lblConnected.setText("disconnected");
        lblConnected.setForeground(Color.red);

        taRead.setColumns(60);
        taRead.setRows(5);
                
        scrollPaneStatus.setViewportView(taRead);

        lblStatus.setText("Status:");

        lblSend.setText("Send:");


        taRead.setEditable(false);
        scrollPaneStatus.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scrollPaneStatus.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

 
        
        
        
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblStatus)
                            .addComponent(scrollPaneStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(67, 67, 67)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        	.addComponent(pbProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        	.addGroup(layout.createSequentialGroup()
                        			.addComponent(btnStart)
                        			.addGap(10, 10, 10)
                        			.addComponent(btnStop))
                            .addGroup(layout.createSequentialGroup()
                            	.addComponent(lblResolution, javax.swing.GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            	.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            	.addComponent(cbResolution, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        	.addComponent(lblSend)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblPort)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbPort, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(btnConnect)
                        .addGap(38, 38, 38)
                        .addComponent(lblConnected)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPort)
                    .addComponent(cbPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConnect)
                    .addComponent(lblConnected))
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblStatus)
                    .addComponent(lblSend))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPaneStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup()
                    		.addComponent(lblResolution, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    		.addComponent(cbResolution, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createSequentialGroup()
                        	.addGroup(layout.createParallelGroup()
                    			.addComponent(btnStart)
	                        	.addComponent(btnStop))
	                        .addGap(18, 18, 18)
	                        .addComponent(pbProgressBar))))
                .addContainerGap(48, Short.MAX_VALUE))
        );

        
        this.setTitle("SerialComm");
        this.setLocation(10, 200);
        this.setResizable(false);
        pack();
    }// </editor-fold>

    private void btnStartListener(java.awt.event.ActionEvent evt) {
    	comm.start(cbResolution.getSelectedIndex());
    	btnStart.setEnabled(false);
    	btnConnect.setEnabled(false);
    	btnStop.setEnabled(true);
    }
    
    private void btnStopListener(java.awt.event.ActionEvent evt) {
    	comm.stop();
    	btnStart.setEnabled(true);
    	btnConnect.setEnabled(true);
    	btnStop.setEnabled(false);
    }

    private void btnConnectListener(java.awt.event.ActionEvent evt) {
        try {
        	if(!connected && cbPort.getItemCount()>0) {
        		comm.connect(cbPort.getSelectedItem().toString());
        		lblConnected.setText("connected");
                lblConnected.setForeground(Color.green );
        		cbPort.setEnabled(false);
        		btnStart.setEnabled(true);
        		btnStop.setEnabled(false);
        		connected = true;
        	}
        	else {
        		comm.disconnect();
        		lblConnected.setText("disconnected");
                lblConnected.setForeground(Color.red);
//        		taRead.setText("");
        		cbPort.setEnabled(true);
        		btnStart.setEnabled(false);
        		btnStop.setEnabled(false);
        		connected = false;
        	}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            	MainWindow window = new MainWindow();
            	window.pack();
                window.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnConnect;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStop;
    private javax.swing.JLabel lblPort;
    private javax.swing.JLabel lblConnected;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblSend;
    private javax.swing.JLabel lblResolution;
    private javax.swing.JScrollPane scrollPaneStatus;
    private javax.swing.JTextArea taRead;
    private javax.swing.JTextField tfPort;
    private JComboBox cbPort;
    private JComboBox cbResolution;
    private JProgressBar pbProgressBar;
    
    private final int[] RESOLUTIONS = {64,128, 256, 512};
    // End of variables declaration
	@Override
	public void update(Observable arg0, Object arg1) {
		while(comm.logMessageAvailabe()) {
			taRead.append(comm.getLogMessage() + "\n");
			taRead.selectAll();
		}
		pbProgressBar.setValue(comm.getProgress());
		if(comm.isFinished()) {
			btnConnect.setEnabled(true);
			btnStart.setEnabled(true);
			btnStop.setEnabled(false);
		}
	}

}
